var attendanceType;
var id;
var groupid;
var iconid;
var subid;
var menuid;
var businessId;
$(function() {
	groupid = ly.getUrlParam("groupid");
	iconid = ly.getUrlParam("iconid");
	subid = ly.getUrlParam("subid");
	menuid = ly.getUrlParam("menuid");

	attendanceType = ly.getUrlParam("attendanceType");
	id = ly.getUrlParam("id");
	if(attendanceType == 2) {
		$(".body_title").html("编辑");
		$(".form").getForm({
			"ajaxform": true,
			id: id
		}, function(data) {
			selectUsersList(data.userId, "", "", "");
			//	selectUsersList(data.userId, "2", "", "1");
			$("#isUsing").getSelect({
				type: "radio",
				asName: "isUsing",
				list: [{
						value: "1",
						display: "是"
					},
					{
						value: "0",
						display: "否"
					}
				],
				default: data.isUsing
			});
			if(data.teamLevel != 1) {
				selectTeamList(data.parentId, data.teamLevel);
			}
			if(data.workerList != null) {
				var work = "";
				$.each(data.workerList, function(i, o) {
					work += o.workerId + ",";
				});
				selectWorkerList(work);
			}
			$("#teamLevel").getSelect({
				type: "radio",
				asName: "teamLevel",
				code: "teamLevel",
				default: data.teamLevel
			});
		});
	} else {
		selectWorkerList("");
		selectUsersList("", "", "", "");
		$("#isUsing").getSelect({
			type: "radio",
			asName: "isUsing",
			list: [{
					value: "1",
					display: "启用"
				},
				{
					value: "0",
					display: "禁用"
				}
			],
			default: '1'
		});
		$("#teamLevel").getSelect({
			type: "radio",
			asName: "teamLevel",
			code: "teamLevel",
			default: 1
		});
	}

	//获取到选择的值
	$("body").on("click", "input[name='teamLevel']", function() {
		var teamLevel = this.value;
		if(parseFloat(teamLevel) > 1) {
			$("#teamParent").removeClass("hide");
			selectTeamList("", teamLevel);
			$('#parentId').addClass('required')
		} else {
			$("#teamParent").addClass("hide");
			$('#parentId').removeClass('required')
		}
	});
	//required
	//提交
	$("body").on("click", "#save-btn-commodityClassification", function(event) {
		event.preventDefault();

		var success = $('.form').validate('submitValidate');
		if(success) {

			//验证通过提交表单
			submitFom();
		}
	})

	//批量删除
	$("#batchDelBtn").click(function() {
		var c = "";
		$("input[name=datachk]:checked").each(function() {
			c += $(this).val() + ',';
		});
		deleteFirstCategory(c);
	});
});

/*
 * 提交表单
 */
function submitFom() {
	var userId = $("#userId").val();
	// if($.trim(userId)=='') {
	// 	ly.toast("请选择负责人");
	// 	return false;
	// }
	if($("input[name=workerId]:checked").length <= 0) {
		ly.toast("请选择工种");
		return false;
	}
	var teamName = $("#teamName").val();
	var teamContext = $("#teamContext").val();
	var parentId = $('select[name=parentId] option:checked').val();
	/*var userId = $('select[name=userId] option:checked').val();*/
	
	var teamLevel = $("#teamLevel input:checked").val();
	var teamCode = $("#teamCode").val();
	var isUsing = $("#isUsing input:checked").val();
	
	var workerList = new Array();
	$("#workerId").each(function(){
		var work = {};
		$("input[name=workerId]:checked").each(function(){
			var c = $(this).val();
			work = {
				'workerId':c
			}
			workerList.push(work);
			work = {};
		});	
	});
	
	var obj = {};
	var url = site.apiUrl + "/back/team/insertOrUpdateTeam";
	if(attendanceType == 1) {
		//新增
		obj = {
			'teamName': teamName,
			'teamContext': teamContext,
			'parentId': parentId,
			'userId': userId,
			'teamCode': teamCode,
			'teamLevel': teamLevel,
			'workerList': workerList,
			'isUsing': isUsing
		};
	} else {
		//修改
		var thisId = $("#id").val();
		obj = {
			'teamName': teamName,
			'teamContext': teamContext,
			'parentId': parentId,
			'userId': userId,
			'teamCode': teamCode,
			'teamLevel': teamLevel,
			'isUsing': isUsing,
			'workerList': workerList,
			'id': thisId
		};
	}
	request({
		url:url,
		data:obj,
		type:"post",
		callback:function(f) {
			console.log(f);
			if(f.code=="rest.success"){
				if(attendanceType == 1){
					ly.alert('保存成功',function(){	            
			    		ly.goto("team-list.html");
			        });
				}else{
					ly.alert('修改成功',function(){	           
			    		ly.goto("team-list.html");
			        });
				}
			}else{
				ly.toast(f.desc);
			}
		}
	});

}

//选择上级部门
function selectTeamList(teamId, teamLevel) {
	$("#teamParent").removeClass("hide");
	var teamLevelNum = teamLevel > 1 ? teamLevel - 1 : teamLevel;
	
	var list = new Array();
	request({
		url: site.gateUrl + "/back/team/selectTeamList",
		data: {
			'num': 1,
			'size': 100,
			'isUsing': 1,
			'teamLevel': teamLevelNum
		},
		type: "get",
		ajaxform: true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var obj = {
					value: ' ',
					display: '请选择'
				};
				list.push(obj);
				if(f.result.list != null && f.result.list != '') {
					obj = {};
					$.each(f.result.list, function(i, o) {
						obj = {
							value: o.id,
							display: o.teamName
						};
						list.push(obj);
					});
				}
				$("#parentId").getSelect({
					type: "select",
					asName: "teamId",
					list: list,
					default: teamId
				});
			}
		}
	});
}

//选择工种
function selectWorkerList(userId) {
	var list = new Array();
	request({
		url: site.gateUrl + "/back/worker/selectWorkerList",
		data: {
			'num': 1,
			'size': 100,
			'isUsing': 1
		},
		type: "get",
		"ajaxform": true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var obj = {};
				if(f.result.list != null && f.result.list != '') {
					obj = {};
					$.each(f.result.list, function(i, o) {
						obj = {
							value: o.id,
							display: o.workerName
						};
						list.push(obj);
					});
				}
				$("#workerId").getSelect({
					type: "checkbox",
					asName: "workerId",
					list: list,
					default: userId
				});
			}
		}
	});
}

//选择用户
function selectUsersList(userId, userType, teamId, assumeOffice) {
	var list = new Array();
	request({
		url: site.gateUrl + "/sUsers/selectUsersList",
		data: {
			'num': 1,
			'size': 1000,
			'isUsing': 1,
			'userType': userType,
			'teamId': teamId,
			'assumeOffice': assumeOffice
		},
		type: "get",
		"ajaxform": true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var obj = {
					value: ' ',
					display: '请选择'
				};
				list.push(obj);
				if(f.result.list != null && f.result.list != '') {
					obj = {};
					$.each(f.result.list, function(i, o) {
						obj = {
							value: o.id,
							display: o.nickName
						};
						list.push(obj);
					});
				}
				$("#userId").getSelect({
					type: "select",
					asName: "userId",
					list: list,
					default: userId
				});
			}
		}
	});
}